﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Linq;
using System.Text;
using System.IO;
using Mimoza.Common;

namespace Mimoza.ObjectLayer
{
    public class Update : INotifyPropertyChanged
    {
        private OL ol;
        private UpdateRecord record;

        public Update(OL ol)
        {
            this.ol = ol;
            this.record = new UpdateRecord();
        }
        public Update(OL ol, UpdateRecord record)
        {
            this.ol = ol;
            this.record = record;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public UpdateRecord Record
        {
            get
            {
                return record;
            }
        }
        public Guid ID
        {
            get { return record.ID; }
        }
        public string Name
        {
            get { return record.Name; }
            set
            {
                if (value != this.record.Name)
                {
                    record.Name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        public string Description
        {
            get { return record.Description; }
            set
            {
                if (value != this.record.Description)
                {
                    record.Description = value;
                    NotifyPropertyChanged("Description");
                }
            }
        }
        public string OS
        {
            get { return record.OS; }
            set
            {
                if (value != this.record.OS)
                {
                    record.OS = value;
                    NotifyPropertyChanged("OS");
                }
            }
        }
        public string Model
        {
            get { return record.Model; }
            set
            {
                if (value != this.record.Model)
                {
                    record.Model = value;
                    NotifyPropertyChanged("Model");
                }
            }
        }
        public string Version
        {
            get { return record.Version; }
            set
            {
                if (value != this.record.Version)
                {
                    record.Version = value;
                    NotifyPropertyChanged("Version");
                }
            }
        }
        public string UpdateFileName
        {
            get { return record.UpdateFileName; }
            set
            {
                if (value != this.record.UpdateFileName)
                {
                    record.UpdateFileName = value;
                    NotifyPropertyChanged("UpdateFileName");
                }
            }
        }
        public Stream UpdateFileBody
        {
            //get { return record.UpdateFileBody; }
            set
            {
                //if (value != this.record.UpdateFileBody)
                {
                    //using (Stream stream = new MemoryStream(value))
                    {
                        UpdatesFileInfo fileInfo = new UpdatesFileInfo(value);

                        record.Name = fileInfo.Name;
                        record.OS = fileInfo.OSName;
                        record.Model = fileInfo.ModelName;
                        record.Version = fileInfo.Version;
                        record.Description = fileInfo.Description;

                        byte[] body = new byte[value.Length - value.Position];
                        value.Read(body, 0, body.Length);

                        record.UpdateFileBody = body;
                    }
                    NotifyPropertyChanged("UpdateFileBody");
                }
            }
        }
    }
}
